package cn.shop.dys.util;

import android.content.Context;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import cn.shop.dys.bean.UserInfo;

/**
 * 用户数据库处理工具
 * Created by sunjian on 2016/4/19.
 */
public class UserInfoDBUtil {
    private static DbUtils dbUtils;

    public static void initDB(Context context,String dbName){
        dbUtils = DbUtils.create(context, dbName);
    }
    public static void saveUserInfo(UserInfo userInfo){
        try {
            UserInfo myUserInfo = dbUtils.findFirst(Selector.from(UserInfo.class).where("userid", "=", userInfo.getUserid()));
            if(myUserInfo==null){
                dbUtils.save(userInfo);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void updateUserInfo(UserInfo userInfo){
        try {
          //  UserInfo myUserInfo = dbUtils.findFirst(Selector.from(UserInfo.class).where("userid", "=", userInfo.getUserid()))
         //   if(myUserInfo==null)return;
            WhereBuilder whereBuilder = WhereBuilder.b("userid", "=", userInfo.getUserid());
            dbUtils.update(userInfo, whereBuilder, new String[]{"nickname","level"});
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static UserInfo queryUserInfoByUserId(String userid){
        UserInfo userInfo=null;
        try {
          //  List<UserInfo> infos= dbUtils.findAll(Selector.from(UserInfo.class).where("userid", "=", userInfo.getUserid()));
           // if(infos==null)return null;
            userInfo = dbUtils.findFirst(Selector.from(UserInfo.class).where("userid", "=", userid));
           // userInfo=infos.get(0);
       //     Log.d("test",userInfo.toString());
        } catch (DbException e) {
            e.printStackTrace();
        }
        return userInfo;
    }
}
